<?php
/*
 * @Author: your name
 * @Date: 2020-12-14 13:45:25
 * @LastEditTime: 2021-01-08 13:51:17
 * @LastEditors: Please set LastEditors
 * @Description: 上传正常规则
 * @FilePath: /special/application/api/service/netfilter/UploadNormalFirewallRuleService.php
 */
namespace app\api\service\netfilter;

use app\common\definition\netfilter\FirewallRuleDefinition;
use app\common\exception\CustomException;
use think\Db;

class UploadNormalFirewallRuleService extends FirewallRuleUploadService
{
    /**
     * @name: 规则上传
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-12-14 13:46:42
     */
    public function upload ()
    {
        $save_data = Db::name('netfilter_firewall_rule_log')->where('type', FirewallRuleDefinition::NORMAL_RULE_GROUP)->where('host_id', $this->host_id)->where('create_time', $this->upload_create_time)->find();

        if (empty($save_data))
        {
            throw new CustomException('未找到规则下发日志', 422);
        }

        if ($save_data['status'] == FirewallRuleDefinition::ARCHIVED_LOG_STATUS)
        {
            throw new CustomException('当前规则已回档!!', 422);
        }
        
        $this->preview_content_id = $save_data['id'];
        $this->preview_content = json_decode($save_data['preview_content'], true);
        $this->upload_create_time = $save_data['create_time'];

        $delete_id_a = [];
        //添加防火墙规则
        $delete_source_firewall_a = $this->getDeleteFirewallRule($this->preview_content, $delete_id_a);
        //修改防火墙规则 添加新增的 删除未应用成功的
       $this->resetFirewallRule($delete_source_firewall_a, $delete_id_a, []);
    }
}